﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PhoneManager.LanguageText;

namespace PhoneManager
{
    public partial class FrmAddPhone : FrmDefault
    {
        private int QtdSIM = 0;
        private int QtdPhone = 0;

        public FrmAddPhone()
        {
            InitializeComponent();
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtName_TextChanged(object sender, EventArgs e)
        {
            if (TxtName.Text.Trim() != "" && TxtNumber.Text.Trim() != "")
            {
                BntAdd.Enabled = true;
            }
            else
            {
                BntAdd.Enabled = false;
                ChkAddP.Checked = false;
                ChkAddS.Checked = false;
            }
            ChkAddP.Enabled = BntAdd.Enabled;
            ChkAddS.Enabled = BntAdd.Enabled;
        }

        private void TxtNumber_TextChanged(object sender, EventArgs e)
        {
            if (TxtName.Text.Trim() != "" && TxtNumber.Text.Trim() != "")
            {
                BntAdd.Enabled = true;
            }
            else
            {
                BntAdd.Enabled = false;
                ChkAddP.Checked = false;
                ChkAddS.Checked = false;
            }
            ChkAddP.Enabled = BntAdd.Enabled;
            ChkAddS.Enabled = BntAdd.Enabled;
        }

        private void TxtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void FrmAddPhone_Load(object sender, EventArgs e)
        {
            foreach (PhoneBook item in SharedLib.PhonesBook)
            {
                if (item.SIMIndex != "")
                {
                    QtdSIM++;
                }
                if (item.PhoneIndex != "")
                {
                    QtdPhone++;
                }
            }
            string txt = QtdPhone.ToString() + "/" + SharedLib.PhoneBookMEMRanger.Max.ToString();
            LblQtdPhone.Text = txt;
            txt = QtdSIM.ToString() + "/" + SharedLib.PhoneBookSIMRanger.Max.ToString();
            LblQtdSIM.Text = txt;
            TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
            TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
        }

        private void ChkAddP_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkAddP.Checked)
            {
                if (QtdPhone + 1 > SharedLib.PhoneBookMEMRanger.Max)
                {
                    SharedLib.MsgAlert(ResourcesText.MsgErrMaxMemoryPhone);
                    ChkAddP.Checked = false;
                }
                if (TxtName.MaxLength > SharedLib.PhoneBookMEMNameMaxLength)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                }
                if (TxtNumber.MaxLength > SharedLib.PhoneBookMEMNumberMaxLength)
                {
                    TxtNumber.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                }
            }
            else
            {
                if (ChkAddS.Checked)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                }
                else
                {
                    TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                    TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                }
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
        }

        private void ChkAddS_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkAddS.Checked)
            {
                if (QtdPhone + 1 > SharedLib.PhoneBookSIMRanger.Max)
                {
                    SharedLib.MsgAlert(ResourcesText.MsgErrMaxMemorySIM);
                    ChkAddS.Checked = false;
                }
                if (TxtName.MaxLength > SharedLib.PhoneBookSIMNameMaxLength)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                }
                if (TxtNumber.MaxLength > SharedLib.PhoneBookSIMNumberMaxLength)
                {
                    TxtNumber.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                }
            }
            else
            {
                if (ChkAddP.Checked)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                }
                else
                {
                    TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                    TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                }
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
        }

        private void BntAdd_Click(object sender, EventArgs e)
        {
            if (!ChkAddP.Checked && !ChkAddS.Checked)
            {
                SharedLib.MsgAlert(ResourcesText.MsgErrSeletStorage);
                return;
            }
            this.Cursor = Cursors.WaitCursor;
            SharedLib.StartWaitProcessThread();

            PhoneBook pb = new PhoneBook();
            pb.Name = TxtName.Text;
            pb.Number = TxtNumber.Text;
            pb.PhoneIndex = "";
            pb.SIMIndex = "";


            bool existNomP = false;
            bool existNomS = false;
            bool existNumP = false;
            bool existNumS = false;
            if (ChkAddP.Checked)
            {
                PhoneBook pbm = SharedLib.FindPhoneByNumber(TxtNumber.Text, false);
                if (pbm != null)
                {
                    pb.PhoneIndex = pbm.PhoneIndex;
                    pb.SIMIndex = pbm.SIMIndex;
                    pb.Name = pbm.Name;
                    TxtName.Text = pb.Name;
                    existNumP = true;
                }
                else
                {
                    pbm = SharedLib.FindPhoneByName(TxtName.Text, false);
                    if (pbm != null)
                    {
                        pb.PhoneIndex = pbm.PhoneIndex;
                        pb.SIMIndex = pbm.SIMIndex;
                        pb.Number = pbm.Number;
                        TxtNumber.Text = pb.Number;
                        existNomP = true;
                    }
                }
            }
            if (ChkAddS.Checked)
            {
                PhoneBook pbm = SharedLib.FindPhoneByNumber(TxtNumber.Text, true);
                if (pbm != null)
                {
                    pb.PhoneIndex = pbm.PhoneIndex;
                    pb.SIMIndex = pbm.SIMIndex;
                    pb.Name = pbm.Name;
                    TxtName.Text = pb.Name;
                    existNumS = true;
                }
                else
                {
                    pbm = SharedLib.FindPhoneByName(TxtName.Text, true);
                    if (pbm != null)
                    {
                        pb.PhoneIndex = pbm.PhoneIndex;
                        pb.SIMIndex = pbm.SIMIndex;
                        TxtNumber.Text = pb.Number;
                        existNomS = true;
                    }
                }
            }
            bool ok = true;
            bool UpdP = false;
            bool UpdS = false;
            if (existNomS || existNomP || existNumS || existNumP)
            {
                if ((ChkAddP.Checked && (existNomP || existNumP)) || (ChkAddS.Checked && (existNomS || existNumS)))
                {
                    string msg = ResourcesText.WordThis;
                    if (existNomS || existNomP)
                    {
                        msg += " " + ResourcesText.WordName ;
                    }
                    if (existNumS || existNumP)
                    {
                        if (existNomS || existNomP)
                        {
                            msg += " " + ResourcesText.WordAnd + " ";
                        }
                        msg += " " + ResourcesText.WordNumber;
                    }
                    msg += " " + ResourcesText.WordAlreadyon + " ";
                    if ((existNomP || existNumP) && (existNomS || existNumS))
                    {
                        ok = false;
                        msg += " " + ResourcesText.WordPhoneandSIM;
                    }
                    else if (!(existNomP || existNumP) && (existNomS || existNumS))
                    {
                        UpdS = true;
                        msg += " " + ResourcesText.SIMNotUpate;
                    }
                    else if ((existNomP || existNumP) && !(existNomS || existNumS))
                    {
                        UpdP = true;
                        msg += " " + ResourcesText.PhoneNotUpate;
                    }
                    SharedLib.MsgAlert(ResourcesText.WordName + ": " + TxtName.Text + "\n" + ResourcesText.WordNumber + ": " + TxtNumber.Text + "\n" + msg);
                }
            }
            if (!ok)
            {
                SharedLib.EndWaitProcessThread();
                this.Cursor = Cursors.Default;
                return;
            }
            bool okP = false;
            bool okS = false;
            int IndexP = -1;
            int IndexS = -1;
            if (ChkAddP.Checked)
            {
                if (UpdP)
                {
                    okP = SharedLib.ModemGSM.PBookPhoneUpdate(pb.PhoneIndex, pb.Name, pb.Number);
                }
                else
                {
                    IndexP = SharedLib.ModemGSM.PBookPhoneInsert(TxtName.Text, TxtNumber.Text);
                    if (IndexP < 0)
                    {
                        SharedLib.MsgErro(SharedLib.ModemGSM.Error);
                    }
                    else
                    {
                        okP = true;
                    }
                }
            }
            if (ChkAddS.Checked)
            {
                if (UpdS)
                {
                    okS = SharedLib.ModemGSM.PBookSimUpdate(pb.SIMIndex, pb.Name, pb.Number);
                }
                else
                {
                    IndexS = SharedLib.ModemGSM.PBookSimInsert(TxtName.Text, TxtNumber.Text);
                    if (IndexS < 0)
                    {
                        SharedLib.MsgErro(SharedLib.ModemGSM.Error);
                    }
                    else
                    {
                        okS = true;
                    }
                }
            }
            if (okP)
            {
                if (!UpdP)
                {
                    pb.PhoneIndex = IndexP.ToString();
                    QtdPhone++;
                }
            }
            if (okS)
            {
                if (!UpdS)
                {
                    pb.SIMIndex = IndexS.ToString();
                    QtdSIM++;
                }
            }
            
            if (okS || okP)
            {
                if (UpdP || UpdS)
                {
                    PhoneBook pbm = null;
                    if (UpdP)
                    {
                        pbm = SharedLib.FindPhoneByID(pb.PhoneIndex, false);
                        pb.SIMIndex = pbm.SIMIndex;
                    }
                    if (UpdS)
                    {
                        pbm = SharedLib.FindPhoneByID(pb.SIMIndex, true);
                        pb.PhoneIndex = pbm.PhoneIndex;
                    }
                }
                else
                {
                    SharedLib.PhonesBook.Add(pb);
                }
                SharedLib.PhonesBook.Sort(new OrderPhoneBook());
                TxtName.Text = "";
                TxtNumber.Text = "";
                TxtName.Focus();
            }

            string txt = QtdPhone.ToString() + "/" + SharedLib.PhoneBookMEMRanger.Max.ToString();
            LblQtdPhone.Text = txt;
            txt = QtdSIM.ToString() + "/" + SharedLib.PhoneBookSIMRanger.Max.ToString();
            LblQtdSIM.Text = txt;

            SharedLib.EndWaitProcessThread();
            this.Cursor = Cursors.Default;
        }
    }
}
